﻿using System.Globalization;
using NHibernate.Sidekick.Data.Domain;
using NHibernate.Sidekick.Services.Interfaces;

namespace NHibernate.Sidekick.Services
{
    /// <summary>
    /// Repository of operations for the underlying data model.
    /// Assumes the type's identity is of type <see cref="int"/>.
    /// </summary>
    /// <typeparam name="T">The specified type.</typeparam>
    public sealed class Service<T> : AbstractService<T, int> where T : DomainObject<T, int>
    {
        #region Thread-safe, lazy Singleton
        /// <summary>
        /// This is a thread-safe, lazy singleton.
        /// </summary>
        public static Service<T> Instance
        {
            get
            {
                return Nested.Service;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Service&lt;T, TId&gt;"/> class.
        /// </summary>
        private Service()
        {
        }

        /// <summary>
        /// Assists with ensuring the thread-safe, lazy singleton.
        /// </summary>
        private class Nested
        {
            internal static readonly Service<T> Service = new Service<T>();
        }
        #endregion


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture, "Service for {0}", typeof(T));
        }
    }

    /// <summary>
    /// Repository of operations for the underlying data model.
    /// </summary>
    /// <typeparam name="T">The specified type.</typeparam>
    /// <typeparam name="TId">The type of identity.</typeparam>
    public sealed class Service<T, TId> : AbstractService<T, TId> where T : DomainObject<T, TId>
    {
        #region Thread-safe, lazy Singleton
        /// <summary>
        /// This is a thread-safe, lazy singleton.
        /// </summary>
        public static Service<T, TId> Instance
        {
            get
            {
                return Nested.Service;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Service&lt;T, TId&gt;"/> class.
        /// </summary>
        private Service()
        {
        }

        /// <summary>
        /// Assists with ensuring the thread-safe, lazy singleton.
        /// </summary>
        private class Nested
        {
            internal static readonly Service<T, TId> Service = new Service<T, TId>();
        }
        #endregion


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture, "Service for {0}", typeof(T));
        }
    }
}
